package com.uroboros.spring.guides.accessing.data.r2dbc;

import java.time.Duration;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringGuidesAccessingDataR2dbcApplication {

	private static final Logger log = LoggerFactory.getLogger(SpringGuidesAccessingDataR2dbcApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(SpringGuidesAccessingDataR2dbcApplication.class, args);
	}

	@Bean
	public CommandLineRunner init(CustomerRepository customerRepository) {
		return args -> {
			// save a few customers
			customerRepository.saveAll(Arrays.asList(
					new Customer("Jack", "Bauer"),
					new Customer("Chloe", "O'Brian"),
					new Customer("Kim", "Bauer"),
					new Customer("David", "Palmer"),
					new Customer("Michelle", "Dessler")))
					.blockLast(Duration.ofSeconds(10));

			// fetch all customers
			log.info("Customers found with findAll():");
			log.info("-------------------------------");
			customerRepository.findAll().doOnNext(customer -> log.info(customer.toString())).blockLast(Duration.ofSeconds(10));

			log.info("");

			// fetch an individual customer by ID
			customerRepository.findById(1L).doOnNext(customer -> {
				log.info("Customer found with findById(1L):");
				log.info("--------------------------------");
				log.info(customer.toString());
				log.info("");
			}).block(Duration.ofSeconds(10));

			// fetch customers by last name
			log.info("Customer found with findByLastName('Bauer'):");
			log.info("--------------------------------------------");
			customerRepository.findByLastName("Bauer").doOnNext(bauer -> {
				log.info(bauer.toString());
			}).blockLast(Duration.ofSeconds(10));;
			log.info("");

		};
	}

}
